/*
 * Copyright (c) Huoty, All rights reserved
 * Author: Huoty <sudohuoty@163.com>
 * CreateTime: 2025-06-10 13:18:33
 *
 */

#include <iostream>
#include <type_traits>
#include <limits>

using namespace std;

template <typename T>
T find_max_in_column(T* arr, const size_t *shape, size_t col_idx);

template <typename T>
T find_max_in_column(T* arr, const size_t *shape, size_t col_idx) {
    size_t rows = shape[0];
    size_t cols = shape[1];

    // 参数校验，空指针和列越界检查
    if (!arr || rows == 0 || col_idx >= cols) return T(0);

    T max_val = arr[col_idx];
    for (size_t i = 1; i < rows; i++) {
        T current = arr[i * cols + col_idx];
        if (current > max_val) {
            max_val = current;
        }
    }
    return max_val;
}

int main(void) {
    // 示例1: 整型数组
    constexpr size_t ROWS = 3, COLS = 4;
    constexpr size_t shape[] = {ROWS, COLS};
    int intArr[ROWS][COLS] = {  // 数组大小需编译期常量
        {1, 5, 3, 4},
        {6, 0, 9, 8},
        {0, 7, 0, 12}
    };
    std::cout << "Int max in col3: "
              << find_max_in_column(&intArr[0][0], shape, 3) << "\n";  // 输出12

    // 示例2: 浮点型数组（动态分配）
    const size_t shape2[2] = {2, 3};
    float* floatArr = new float[shape2[0] * shape2[1]] {
        1.1f, 0.0f, 5.5f, 0.0f, 4.4f, 3.3f
    };
    std::cout << "Float max in col1: "
              << find_max_in_column(floatArr, shape2, 1) << "\n";  // 输出4.4
    delete[] floatArr;

    return 0;
}
